package com.iona.utils;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;

import org.hibernate.Transaction;
import org.hibernate.classic.Session;

import com.iona.domain.BaseEntity;
import com.iona.domain.Purview;
import com.iona.domain.Discipline;

public class Storage {

	public void storeAll(List<BaseEntity> toCreate, List<BaseEntity> toUpdate,
			List<BaseEntity> toDelete) {
		toDelete = (toDelete == null) ? Collections.EMPTY_LIST : toDelete;
		toCreate = (toCreate == null) ? Collections.EMPTY_LIST : toCreate;
		toUpdate = (toUpdate == null) ? Collections.EMPTY_LIST : toUpdate;

		insert(toCreate);
		update(toUpdate);
		// remove(toDelete);
	}

	private void insert(List<BaseEntity> toCreate) {
		if (!toCreate.isEmpty()) {
			for (BaseEntity object : toCreate) {
				persist(object);
			}
		}
	}

	private void update(Collection<BaseEntity> objects) {
		if (!objects.isEmpty()) {
			for (BaseEntity bo : objects) {
				store(bo);
			}
		}
	}

	private void store(BaseEntity bo) {
		Session session = HibernateUtils.getSessionFactory().openSession();
		session.save(bo);
	}

	private void persist(BaseEntity bo) {
		Session session = HibernateUtils.getSessionFactory()
				.getCurrentSession();
		session.persist(bo);

	}

}
